package com.supersit.GZFDA.util;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.supersit.GZFDA.activity.SearchDataListActivity;
import com.supersit.GZFDA.app.MyApp;
import com.supersit.GZFDA.db.LoadDBData;
import com.supersit.GZFDA.entity.Business;

public class AddressSearchData {
	private Context context;

	public AddressSearchData(Context context) {
		super();
		this.context = context;
	}

	private Cursor searchAddress(String constraint) {
		SQLiteDatabase db = LoadDBData.openDatabase(context);
		String selection = "select * from gzfad_data where operate_address like '%"
				+ constraint.trim() + "%' order by operate_address";
		return db.rawQuery(selection, null);
	}

	public void getForResult(String constraint) {
		ProgressDialog pd = ProgressDialog.show(context, "", "...正在搜索数据...");
		Cursor cursor = searchAddress(constraint);
		List<Business> businessList = new ArrayList<Business>();
		while (cursor.moveToNext()) {
			Business business = new Business();
			business.setId(cursor.getString(cursor.getColumnIndex("id")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("id")));
			business.setBusiness_name(cursor.getString(cursor
					.getColumnIndex("business_name")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("business_name")));
			business.setBusiness_type(cursor.getString(cursor
					.getColumnIndex("business_type")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("business_type")));
			business.setContacts(cursor.getString(cursor
					.getColumnIndex("contacts")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("contacts")));
			business.setData_of_issue(cursor.getString(cursor
					.getColumnIndex("date_of_issue")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("date_of_issue")));
			business.setExpiry_data(cursor.getString(cursor
					.getColumnIndex("expiry_date")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("expiry_date")));
			business.setLegal_person(cursor.getString(cursor
					.getColumnIndex("legal_person")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("legal_person")));
			business.setLicense_key(cursor.getString(cursor
					.getColumnIndex("license_key")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("license_key")));
			business.setOperate_addrress(cursor.getString(cursor
					.getColumnIndex("operate_address")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("operate_address")));
			business.setQuality_principal(cursor.getString(cursor
					.getColumnIndex("quality_principal")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("quality_principal")));
			business.setRange(cursor.getString(cursor.getColumnIndex("range")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("range")));
			business.setRegister_address(cursor.getString(cursor
					.getColumnIndex("register_address")) == null ? "" : cursor
					.getString(cursor.getColumnIndex("register_address")));
			business.setStreet(cursor.getString(cursor.getColumnIndex("street")) == null ? ""
					: cursor.getString(cursor.getColumnIndex("street")));
			businessList.add(business);
		}
		pd.dismiss();
		MyApp.getInstance().setBusinesss(businessList);
		if (businessList != null && businessList.size() > 0) {
			Intent intent = new Intent(context, SearchDataListActivity.class);
			context.startActivity(intent);
			Log.d("tag", "搜索数据开始跳转");
		} else {
			Toast.makeText(context, "没有搜索到\"" + constraint + "\"相关的数据", 1)
					.show();
		}

	}
}
